package com.talentgame.tankspk.GameCore;


import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.model.Node;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.physics.bullet.collision.btBoxShape;
import com.badlogic.gdx.physics.bullet.collision.btCollisionShape;
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
import com.badlogic.gdx.utils.Disposable;

/**
 * Created by 54560 on 2016/12/18.
 */
public class GameObject extends ModelInstance implements Disposable{
    public btCollisionShape shape;
    public btRigidBody body;
    public btRigidBody.btRigidBodyConstructionInfo constructionInfo;
    public MyMotionState motionState;
    public float mass;
    public Vector3 localInertia = new Vector3();
    public int uniqueId=-1;

    public GameObject(ModelInstance copyFrom) {
        super(copyFrom);
    }

    public GameObject(Model model, String nodeId) {
        super(model, nodeId);
        caculateNode(nodeId);
        caculateBt();
        //instantiate(true);
    }

    public void update(float detalTime)
    {

    }
    /*
* 如果该节点是一个模型下的Node,则需要计算
* */
    protected void caculateNode(String nodeId)
    {
        Node node = this.getNode(nodeId);
        transform.set(node.globalTransform);
        node.translation.set(0,0,0);
        node.scale.set(1,1,1);
        node.rotation.idt();
        calculateTransforms();
    }
    //计算碰撞盒的大小，默认是
    protected void caculateBt()
    {
        //计算模型大小，注意与自己的Transform有关
        motionState = new MyMotionState();
        motionState.transform = transform;
        constructionInfo = getConstructionInfo();
        body = new btRigidBody(constructionInfo);
        body.setMotionState(motionState);
        body.proceedToTransform(this.transform);
    }
    protected btRigidBody.btRigidBodyConstructionInfo getConstructionInfo()
    {
        BoundingBox box = new BoundingBox();
        calculateBoundingBox(box);
        float h= box.getHeight()/2;
        shape = new btBoxShape(new Vector3(box.getWidth()/2,h,box.getDepth()/2));
        //SetMass(1);
        if(mass > 0)
            shape.calculateLocalInertia(mass,localInertia);
        else
            localInertia = new Vector3(0,0,0);
        return new btRigidBody.btRigidBodyConstructionInfo(mass,motionState,shape,localInertia);
    }
    protected void SetMass(float mass)
    {
        this.mass=mass;
    }
    public GameObject(Model model) {
        super(model);
        caculateBt();
    }
    /*
    * 渲染出该物体
    * */
    public static void instantiate(GameObject go, boolean isColider)
    {
        //go.uniqueId = TanksGame.instance.renderList.size();
        go.uniqueId = TanksGame.instance.UId++;
        if(isColider)
        {
            TanksGame.instance.collisionWorld.addRigidBody(go.body);
            //碰撞后识别的Id
            go.body.setUserValue(go.uniqueId);
        }
        TanksGame.instance.renderList.put(go.uniqueId,go);
    }
    public static void instantiate(GameObject go,boolean isColider,Vector3 position,Quaternion rotation)
    {
        go.transform.setToTranslation(position);
        go.transform.rotate(rotation);
        go.body.proceedToTransform(go.transform);
        instantiate(go,isColider);
    }
    public static void Destory(GameObject go)
    {
        Gdx.app.log("销毁","销毁:" + go.uniqueId);
        TanksGame.instance.collisionWorld.removeRigidBody(go.body);
        TanksGame.instance.renderList.remove(go.uniqueId);
        go.body.setCollisionFlags(0);
        go.body.setContactCallbackFilter(0);
        go.dispose();
    }

    public Vector3 getPosition()
    {
        Vector3 pos = new Vector3();
        transform.getTranslation(pos);
        return pos;
    }
    /*
    * 获取矩阵正前方的向量
    * */
    public Vector3 getForward(Matrix4 transform)
    {
        Quaternion ro = new Quaternion();
        transform.getRotation(ro);
        Matrix4 temp = transform.cpy().idt().rotate(ro);
        Vector3 forward =  Vector3.Z.cpy().rot(temp).cpy();
        return  forward;
    }


    @Override
    public void dispose() {
        motionState.dispose();
        constructionInfo.dispose();
        body.dispose();
        shape.dispose();

    }
}
